package com.zhida.chatservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhida.common.context.UserContext;
import com.zhida.common.entity.Announcement;
import com.zhida.chatservice.mapper.AnnouncementMapper;
import com.zhida.chatservice.service.IAnnouncementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhida.common.entity.Result;
import com.zhida.common.utils.ObjectUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ling
 * @since 2024-07-23
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements IAnnouncementService {

    @Resource
    AnnouncementMapper announcementMapper;
    @Resource
    RedisTemplate<String,String> redisTemplate;

    @Override
    public Result<?> createAnnouncement(Announcement announcement) {
        announcement.setUsername(UserContext.getUser());
        return Result.returnByInt(announcementMapper.insert(announcement),"发布公告成功","发布公告失败");
    }

    @Override
    public Result<?> deleteAnnouncement(@PathVariable Integer id) {
        UpdateWrapper<Announcement> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status",-1).eq("id",id);
        return Result.returnByInt(announcementMapper.update(updateWrapper),"删除公告成功","删除公告失败");
    }
    @Override
    public Result<?> getAnnouncementById(Integer id) throws JsonProcessingException {
        Announcement announcement = announcementMapper.selectOne(new QueryWrapper<Announcement>().allEq((Map.of("id", id, "status", 1))));
        return Result.success(announcement);
    }

    @Override
    public Result<?> updateAnnouncement(Announcement announcement) {
        UpdateWrapper<Announcement> updateWrapper = new UpdateWrapper<>();
        if (StringUtils.isNotBlank(announcement.getTitle())){
            updateWrapper.set("title" , announcement.getTitle());
        }
        if (StringUtils.isNotBlank(announcement.getContent())){
            updateWrapper.set("content" , announcement.getContent());
        }
        updateWrapper.eq("id",announcement.getId());
        return Result.returnByInt(announcementMapper.update(updateWrapper),"更新公告成功","更新公告失败");
    }

    @Override
    public Result<?> pushAnnouncement(Integer id) {
        return null;
    }

    @Override
    public Result<?> getLatestAnnouncement(Integer group_id) {
        return Result.success(announcementMapper.selectOne(new QueryWrapper<Announcement>().allEq(Map.of("group_id",group_id,"status",1))));
    }
}
